﻿using System;
using System.Collections.Generic;
using System.Text;
using CommunityServer.WebServices.MediaGalleries;
using Microsoft.Win32;
using System.Security.AccessControl;

namespace nb.development.csmediamanager
{
    public class Site
    {
        public Site(Guid id)
        {
            RegistryKey key1 = Registry.CurrentUser.OpenSubKey("SOFTWARE\\nb development\\CS Media Manager\\Site-" + id.ToString(), true);
            this.id = id;
            this.url = (string)key1.GetValue("Url");
            this.user = (string)key1.GetValue("User");
            this.key = (string)key1.GetValue("Key");
            this.name = (string)key1.GetValue("Name");
            this.collapsed = Convert.ToBoolean(key1.GetValue("Collapsed", false));
        }

        public Site(Guid id, string url, string key, string user, string name)
        {
            this.id = id;
            this.url = url;
            this.key = key;
            this.user = user;
            this.name = name;
            this.collapsed = false;
        }

        private bool collapsed;
        public bool Collapsed
        {
            get { return collapsed; }
            set { collapsed = value; Save(); }
        }

        private Guid id;
        public Guid Id
        {
            get { return id; }
        }

        private string url;
        public string Url
        {
            get { return url; }
            set { url = value; }
        }

        private string key;
        public string Key
        {
            get { return key; }
            set { key = value; }
        }

        private string user;
        public string User
        {
            get { return user; }
            set { user = value; }
        }

        public MediaGalleriesService Service
        {
            get { return new MediaGalleriesService(this.url, this.user, this.key); }
        }

        private string name;
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

        private Dictionary<int, MediaGalleryGroup> groups;

        public Dictionary<int, MediaGalleryGroup> Groups
        {
            get { return groups; }
            set { groups = value; }
        }
        private Dictionary<int, MediaGallery> galleries;

        public Dictionary<int, MediaGallery> Galleries
        {
            get { return galleries; }
            set { galleries = value; }
        }
        private Dictionary<int, MediaGalleryPost> posts;

        public Dictionary<int, MediaGalleryPost> Posts
        {
            get { return posts; }
            set { posts = value; }
        }

        public void Save()
        {
            RegistryKey key;
            if (isRegistered()) key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\nb development\\CS Media Manager\\Site-" + this.id.ToString(), true);
            else
                key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\nb development\\CS Media Manager\\Site-" + this.id.ToString(), RegistryKeyPermissionCheck.ReadWriteSubTree);
            key.SetValue("Url", this.url);
            key.SetValue("User", this.user);
            key.SetValue("Key", this.key);
            key.SetValue("Name", this.name);
            key.SetValue("Collapsed", this.collapsed);
        }

        public void Delete()
        {
            if (isRegistered()) Registry.CurrentUser.DeleteSubKeyTree("SOFTWARE\\nb development\\CS Media Manager\\Site-" + this.id.ToString());
        }

        private bool isRegistered()
        {
            if (Registry.CurrentUser.OpenSubKey("SOFTWARE\\nb development\\CS Media Manager\\Site-" + this.id.ToString(), RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey) != null) return true;
            return false;
        }

    }
}
